
package com.dy.run;
/**
 * @Description:
 * @Date：2018年6月29日
 * @author： DongY
 * @version ：1.0
 * 	  1.[2018年6月29日]CreatBy：dy
 */

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.dy.model.ClassAndPackage;
import com.dy.model.Creator;
import com.dy.model.TableInfo;
import com.dy.template.BaseSting;
import com.dy.template.Common;
import com.dy.template.HtmlAddPage;
import com.dy.template.ListJs;
import com.dy.template.Model;
import com.dy.template.Sql;

import cn.dy.util.FileUtil;
import cn.dy.util.StringUtil;
import lombok.Data;

@Data
public class CreatManager {
	private TableInfo tableInfo;

	private ClassAndPackage p;

	public static String baseDir;
	private Map<String, Creator> map = new HashMap<>();
	{
		// ---------------初始化各项参数--------------
		// final String basePath = System.getProperty("user.dir");
		Creator creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/webapp/views/form");
		creator.setTemplatePath("com/dy/template/htmlEdit.tl");
		creator.setClazz(HtmlAddPage.class);
		creator.setOutName("${className}Edit.html");
		map.put("form", creator);

		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/webapp/views");
		creator.setTemplatePath("com/dy/template/htmlList.tl");
		creator.setClazz(Common.class); // creator.setClazz(HtmlList.class);
		creator.setOutName("${className}.html");
		map.put("list", creator);

		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/webapp/static/js");
		creator.setTemplatePath("com/dy/template/listJs.tl");
		creator.setClazz(ListJs.class);
		creator.setOutName("${className}.js");
		map.put("listJs", creator);

		// ------------------------类文件----------------------------
		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/java/com/hyst/domain");
		creator.setTemplatePath("com/dy/template/model.tl");
		creator.setClazz(Model.class);
		creator.setOutName("${className}.java");
		map.put("model", creator);

		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/java/com/hyst/web");
		creator.setTemplatePath("com/dy/template/controller.tl");
		creator.setClazz(Common.class);
		creator.setOutName("${className}Controller.java");
		map.put("controller", creator);

		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/java/com/hyst/service");
		creator.setTemplatePath("com/dy/template/service.tl");
		creator.setClazz(Common.class);
		creator.setOutName("${className}Service.java");
		map.put("service", creator);

		creator = new Creator();
		// 将错就错了暂时 serviceImpl包在项目里写错成了seviceImpl；
		creator.setOutPath(baseDir + "/src/main/java/com/hyst/seviceImpl");
		creator.setTemplatePath("com/dy/template/serviceImpl.tl");
		creator.setClazz(Common.class);
		creator.setOutName("${className}ServiceImpl.java");
		map.put("serviceImpl", creator);

		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/java/com/hyst/repository");
		creator.setTemplatePath("com/dy/template/dao.tl");
		creator.setClazz(Common.class);
		creator.setOutName("${className}Mapper.java");
		map.put("dao", creator);

		creator = new Creator();
		creator.setOutPath(baseDir + "/src/main/resources/mapper");
		creator.setTemplatePath("com/dy/template/sqlMapper.tl");
		creator.setClazz(Sql.class);
		creator.setOutName("${className}Mapper.xml");
		map.put("sql", creator);
	}

	public CreatManager(final TableInfo tableInfo) {
		this.tableInfo = tableInfo;
	}

	public CreatManager(final TableInfo tableInfo, final ClassAndPackage classAndPackage) {
		this.tableInfo = tableInfo;
		p = classAndPackage;
	}

	public void docreat() throws InstantiationException, IllegalAccessException, IOException {
		final Collection<Creator> collection = map.values();
		final Iterator<Creator> it = collection.iterator();
		System.err.println("-------------------------------------------------------------");
		while (it.hasNext()) {
			final Creator creator = it.next();
			final BaseSting baseSting = creator.getClazz().newInstance();
			String resource = baseSting.getStr(tableInfo, creator);
			resource = p.doreplace(resource);
			// System.out.println(resource);
			String outPath = creator.getOutPath();
			outPath += "/" + creator.getOutName().replace("${className}", tableInfo.getClassName());
			FileUtil.write(resource, outPath);
			System.err.println("成功生成文件:" + outPath);
			System.err.println("-------------------------------------------------------------");
		}
		System.err.println("-------------《将错就错版》请注意新增、修改时时间问题---------------");
	}

	/**
	 * 或略的生成列表
	 * 
	 * @Description:
	 * @param names
	 *            void
	 */
	public void ingnor(final List<String> names) {
		if (map == null || names == null || names.size() == 0) {
			return;
		}
		for (final String name : names) {
			map.remove(name);
		}
	}

	/**
	 * @Description:
	 * @param string
	 *            void
	 */
	public void ingnor(final String string) {
		if (map == null || StringUtil.isEmpty(string)) {
			return;
		}
		map.remove(string);
	}

}
